package sg.edu.nus.iss.club;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class Club {

	private List<Member> members;
	private Map<String, Facility> facilities;
	private BookingRegister bookings;
	
    private int    numMembers = 1;
	
	public Club() {
		members = new ArrayList<Member>();
		facilities = new HashMap<String, Facility>();
		this.bookings = new BookingRegister();
	}

    public Member getMember (int memberNum) {
    	for (Member aMember : members) {
    		if (memberNum == aMember.getMemberNumber()) {
    			return aMember;
    		}
    	}
    	return null;
    }

    public List<Member> getMembers () {
    	List<Member> result = (List<Member>)((ArrayList<Member>)members).clone();
    	return result;
    }

    public Member addMember (String surname, String firstName,
                                             String secondName) {
        Member m = new Member (surname, firstName, secondName,
                                                   numMembers);
        numMembers++;
        members.add(m);
        return m;
    }

    public void removeMember (int memberNum) {
    	Member removeMember = null;
    	for (Member aMember : members) {
    		if (memberNum == aMember.getMemberNumber()) {
    			removeMember = aMember;
    			numMembers--;
    		}
    	}
    	members.remove(removeMember);
    }

    public void showMembers () {
        List<Member> memberList = getMembers();
        for (Member aMember : memberList) {
            aMember.show();
        }
    }
    
    public Facility getFacility(String name) {
    	return facilities.get(name);
    }
    
    public List<Facility> getFacilities() {
    	List<Facility> result = new ArrayList<Facility>();
    	Iterator<String> iter = facilities.keySet().iterator();
    	while (iter.hasNext()) {
    		result.add(facilities.get(iter.next()));
    	}
    	return result;
    }
    
    public void addFacility(String name, String description) {
    	Facility facility = new Facility(name, description);
    	facilities.put(facility.getName(), facility);
    }
    
    public void removeFacility(String name) {
    	facilities.remove(name);
    }
    
    public void showFacilities() {
    	List<Facility> facilityList = getFacilities();
    	for (Facility aFacility : facilityList) {
    		aFacility.show();
    	}
    }
    
    public void addBooking(int memberNumber, String facName, Date startDate, Date endDate) throws BadBookingException {
    	Member m = getMember(memberNumber);
    	Facility fac = getFacility(facName);
    	bookings.addBooking(m, fac, startDate, endDate);
    }
    
    public void removeBooking(Booking booking) {
    	bookings.removeBooking(booking);
    }
    
    public List<Booking> getBookings(Facility facility, Date date) {
    	Calendar cal_start = Calendar.getInstance();
    	Calendar cal_end = Calendar.getInstance();
    	cal_start.setTime(date);
    	cal_start.set(Calendar.HOUR_OF_DAY, 0);
    	cal_start.set(Calendar.MILLISECOND, 0);
    	cal_start.set(Calendar.MINUTE, 0);
    	cal_start.set(Calendar.SECOND, 0);
    	cal_end.setTime(date);
    	cal_end.set(Calendar.HOUR_OF_DAY, 23);
    	cal_end.set(Calendar.MILLISECOND, 999);
    	cal_end.set(Calendar.MINUTE, 59);
    	cal_end.set(Calendar.SECOND, 59);
    	return bookings.getBookings(facility, cal_start.getTime(), cal_end.getTime());
    }
    

}
